package com.example.mptnet;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.example.mptnet.Interfaces.API;
import com.example.mptnet.Models.Mailing;
import com.example.mptnet.Models.User;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class Profile extends Fragment {
    Retrofit retrofit;
    API api;
    SharedPreferences settings;
    User user = new User();
    Mailing mailing = new Mailing();
    String UID = "";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_profile, container, false);
        settings = getContext().getApplicationContext().getSharedPreferences("SETTINGS", Context.MODE_PRIVATE);
        retrofit = Builder.request();
        api = retrofit.create(API.class);
        UID = settings.getString("UID","");
        Call<User> calluser = api.GetUser(UID);
        calluser.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                if(response.isSuccessful()){
                    user = response.body();
                    ((TextView)view.findViewById(R.id.dateregistration)).setText(user.getDateregistration());
                    ((TextView)view.findViewById(R.id.firsname)).setText(user.getFirstname());
                    ((TextView)view.findViewById(R.id.username)).setText(user.getUsername());
                    ((TextView)view.findViewById(R.id.UID)).setText(UID);
                    ((TextView)view.findViewById(R.id.group)).setText(user.getUsers_group().equals("default") ? "Не зарегестрирована" : user.getUsers_group());
                }
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {

            }
        });
        Call<Mailing> callmailing = api.GetMailing(UID);
        callmailing.enqueue(new Callback<Mailing>() {
            @Override
            public void onResponse(Call<Mailing> call, Response<Mailing> response) {
                if (response.isSuccessful()){
                    mailing = response.body();
                    ((TextView)view.findViewById(R.id.mailing)).setText(mailing.getTime().equals("") ? "Не зарегестрирована" : mailing.getTime());
                }
            }

            @Override
            public void onFailure(Call<Mailing> call, Throwable t) {

            }
        });
        view.findViewById(R.id.updatedata).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().beginTransaction().replace(R.id.fragment_container, new Settings()).commit();
            }
        });
        return view;
    }
}